<?
if($_FILES['photo']['name'])
{
	$siztmbnew['news'] = '';
	$siztmbnew['product'] = '166';
	$siztmbnew['promotion'] = '1';
	$siztmbnew['firstslide'] = '166';
	$widthPic = "1019";
	$heightPic = "565";
	
	
	$newname = explode(".",$_FILES['photo']['name']);
	$ext = $newname[(sizeof($newname)-1)];
	$newname = date("YmdHis").rand(1000,9999).".".$ext;
	$maxorder = Q1("select MAX(img_order) from images where na_id = '".$_REQUEST['na_id']."' ");
    imgResizeSave("../imgs/",$newname,$siztmbnew[$_REQUEST['tables']],$_FILES["photo"]["tmp_name"]);
	
	
	$valuearray['img_name1'] = $_POST['img_name1'];
	$valuearray['img_pic'] = $newname;
	$valuearray['na_id'] = $_REQUEST['na_id'];
	$valuearray['img_order'] = $maxorder[0]+1;
	$valuearray['img_ip'] = $_SERVER['REMOTE_ADDR'];
	$valuearray['tables'] = $_REQUEST['tables'];
	$valuearray['img_keyword'] = $_REQUEST['img_keyword'];
	$valuearray['img_description'] = $_REQUEST['img_description'];
	Qinsert("images","",$valuearray);
}
else if($_REQUEST['swap1'] >= 1 && $_REQUEST['swap2'] >= 1)
{
		$r1 = Q1("select img_id,img_order from images  where img_id =   ".$_REQUEST['swap1']);
		$r2 = Q1("select img_id,img_order from images  where img_id =   ".$_REQUEST['swap2']);
		mysql_query("update images set img_order = '".$r2['img_order']."' where img_id = '".$r1['img_id']."'  ");
		mysql_query("update images set img_order = '".$r1['img_order']."' where img_id = '".$r2['img_id']."'  ");
}
else if($_REQUEST['delid'] >= 1) 
{
	$del = Q1("select img_pic from images where img_id =   ".$_REQUEST['delid']);
	mysql_query("DELETE FROM `images` WHERE `images`.`img_id` = '".$_REQUEST['delid']."' LIMIT 1");
	@unlink("../imgs/".$del['img_pic']);
	@unlink("../imgs/".str_replace(".","-swap.",$del['img_pic']));
	@unlink("../imgs/".str_replace(".","-tmp.",$del['img_pic']));
	
}
$gh['product'] = "Color";
$gh['firstslide'] = "Desception";
?>
<SCRIPT LANGUAGE="JavaScript">
	var img_name11 = 0;
	function ssdsd()
	{
		if(img_name11 == 0)
		{
		alert('Error! Please upload yout image.');
		return false;
		}
		else return true;
	}
</SCRIPT>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" onsubmit="javascript:return ssdsd();">
<label>Image file</label><input name="photo" type="file" onchange="javascript:img_name11='1';" />
<label>Image <?=$gh[$_REQUEST['tables']]?></label><input name="img_name1" id="img_name1" type="text" />
<label>Image keyword </label><input name="img_keyword" id="img_name1" type="text" />
<label>Image description</label><input name="img_description" id="img_name1" type="text" />

<br /><br />
<input name="" type="submit" value="<?=btsubmit()?>" />
</form>


<fieldset>
<legend>All Images</legend>
    
	<?
	$k=0;
    $r1 = Q2("select img_pic,img_id,img_name1 from images where tables = '".$_REQUEST['tables']."' and na_id = '".$_REQUEST['na_id']."' order by img_order desc ");
	foreach($r1 as $r) {$orderingg[] = $r['img_id'];}
    foreach($r1 as $r)
    {
    ?>
		<div style="border:#CCCCCC 1px solid; float:left; padding:2px 2px;text-align:center;">
        <img src="../imgs/<?=$r['img_pic']?>" height="70"><br />
		<?=$r['img_name1']?><br />
        
        <? if($orderingg[($k-1)] > 0) { ?>
        <a href="?body=addpic&na_id=<?=$_REQUEST['na_id']?>&page=<?=$_REQUEST['page']?>&tables=<?=$_REQUEST['tables']?>&swap1=<?=$orderingg[$k]?>&swap2=<?=($orderingg[($k-1)])?>"><img src="images/lnarrow.png" width="10" height="10" alt="Move up" title="Move up"  <?=btswap()?> border="0" ></a>&nbsp;					&nbsp;
		<? } ?>
        <a href="?body=addpic&na_id=<?=$_REQUEST['na_id']?>&page=<?=$_REQUEST['page']?>&tables=<?=$_REQUEST['tables']?>&delid=<?=$r['img_id']?>"  onclick="javascript:if(confirm('Are you sure you want to delete')){return true;} else{return false;}" > <img src="images/cancel_f2.png" alt="Delete" title="Delete"  width="15" height="15" border="0" <?=btswap()?> > </a>
        <? if($orderingg[($k+1)] > 0) { ?>
        &nbsp;&nbsp;&nbsp;<a href="?body=addpic&na_id=<?=$_REQUEST['na_id']?>&page=<?=$_REQUEST['page']?>&tables=<?=$_REQUEST['tables']?>&swap1=<?=$orderingg[$k]?>&swap2=<?=($orderingg[($k+1)])?>"><img src="images/rnarrow.png" width="10" height="10" alt="Move down" title="Move down"  <?=btswap()?> border="0" ></a>
		<? } ?>
        
        </div>
    <?
	$k++;
    }
    
    ?>
</fieldset>